import models.CheckPower;
import models.ReturnJson;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 * Created by yangxianming on 2015-11-02.
 * 权限过滤
 */
public class PowerFilter implements Filter {
    private ArrayList<String> business = new ArrayList<String>();
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        String s = filterConfig.getServletContext().getRealPath("/")+"WEB-INF/classes";
        System.out.println("----------PowerFilter init----------" + s);
        File file = new File(s);
        for (String one: file.list()){
            if (one.contains("Business")){
                //把src目录中的类文件对应 *Business 的目录都是业务功能，对这些目录做权限控制
                business.add(one);
            }
        }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        ReturnJson returnJson;
        response.setHeader("Content-type", "text/html;charset=UTF-8");
        String uri = request.getRequestURI();

        //接口白名单
        ArrayList<String> white = new ArrayList<String>();
        white.add("/progress/showProgress");
        white.add("/configuration/dataCatch");
        white.add("/caseManager/saveReport");
        white.add("/caseManager/loadReport");
        white.add("/configuration/dataManager");
        white.add("/survey/surveyUser");
        white.add("/survey/surveyUserSave");
        white.add("/mobNodes/mobNodesLogManger");
        white.add("/apiTestResult/queryApiTestResult");

        if (!white.contains(uri)){
            //判断是否需要验证
            String[] uriArr = uri.split("/");
            if (uriArr.length >= 3){
                String mainClassName = uriArr[1];
                if (business.contains(mainClassName + "Business")){
                    //权限验证
                    char[] cs = uriArr[2].toCharArray();
                    cs[0] -= 32;
                    String nextClassName = String.copyValueOf(cs);
                    returnJson = CheckPower.checkPower(request, mainClassName + "Business", nextClassName);
                    if (returnJson.getApiStatus() == 0){
                        //PrintWriter写在这里可以确保数据流使用后关闭
                        PrintWriter out = response.getWriter();
                        out.print(returnJson.toString());
                        return;
                    }
                }
            }
        }else {
            System.out.println("白名单:"+uri);
        }

        servletRequest.setCharacterEncoding("UTF-8");
        filterChain.doFilter(servletRequest, servletResponse);
        servletResponse.setCharacterEncoding("UTF-8");
    }

    @Override
    public void destroy() {

    }
}
